﻿using BillUpload.Model;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace BillUpload.Util
{
    class HttpUtil
    {

        /// <summary>
        /// 获取平台店铺数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="jsonStr"></param>
        public static ErpResult GetPlatShopList(String url, String jsonStr)
        {
            return SubmitData(url, jsonStr);
        }

        /// <summary>
        /// 添加平台店铺数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="jsonStr"></param>
        public static ErpResult AddPlatShopList(String url, String jsonStr)
        {
            return SubmitData(url, jsonStr);
        }


        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="jsonStr"></param>
        public static ErpResult PutBill(String url, String jsonStr)
        {
            return SubmitData(url, jsonStr);
        }


        private static ErpResult SubmitData(String url, String jsonStr)
        {

            // 1. 创建请求对象     
            var request = WebRequest.Create(url);
            request.Method = "POST";

            // 2. 设置请求头
            request.ContentType = "application/json";
            request.Headers.Add("BillAuthorization", TokenUtil.GetToken(jsonStr)); // 可选认证头

            // 3. 准备JSON数据    
            byte[] dataBytes = Encoding.UTF8.GetBytes(jsonStr);

            // 4. 配置请求流
            request.ContentLength = dataBytes.Length;
            try
            {
                // 5. 写入请求体
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(dataBytes, 0, dataBytes.Length);
                }

                // 6. 获取响应
                using (var response = (HttpWebResponse)request.GetResponse())
                using (var responseStream = new StreamReader(response.GetResponseStream()))
                {
                    var responseText = responseStream.ReadToEnd();
                    Console.WriteLine($"状态码: {response.StatusCode}");
                    Console.WriteLine($"响应内容: {responseText}");
                    ErpResult erpResult = JsonUtil.FromJSON<ErpResult>(responseText);
                    if (null == erpResult)
                    {
                        MessageBox.Show("服务器返回[null]", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return erpResult;
                }
            }
            catch (WebException ex)
            {
                // 7. 错误处理
                if (ex.Response is HttpWebResponse errorResponse)
                {
                    using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                    {
                        var errorText = reader.ReadToEnd();
                        Console.WriteLine($"错误状态码: {errorResponse.StatusCode}");
                        Console.WriteLine($"错误详情: {errorText}");
                    }
                }
                MessageBox.Show($"请求异常: {ex.Message}");
                return new ErpResult()
                {
                    message = ex.Message,
                    success = false
                };
            }
        }
    }
}